/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kylingroup.J2SE.Thread;

/**
 *
 * @author kylin
 */
public class TestThreadDeadLock implements Runnable{
    public int flag=1;
    static Object o1=new Object(),o2=new Object();
    public void run(){
        System.out.println("flag="+flag);
        if(flag==1){
            synchronized(o1){
                try{
                    Thread.sleep(500);
                }catch(Exception e){
                    e.printStackTrace();
                }
                synchronized(o2){
                    System.out.println("1");
                }
            }
        }
        if(flag==0){
            synchronized(o2){
                try{
                    Thread.sleep(500);
                }catch(Exception e){
                    e.printStackTrace();
                }
                synchronized(o1){
                    System.out.println("0");
                }
            }
        }
    }
    public static void main(String args[]){
        TestThreadDeadLock td1=new TestThreadDeadLock();
        TestThreadDeadLock td2=new TestThreadDeadLock();
        td1.flag=1;
        td2.flag=0;
        Thread t1=new Thread(td1);
        Thread t2=new Thread(td2);
        t1.start();
        t2.start();
    }
}
